package com.custom.base.view

import android.content.Context
import android.content.res.TypedArray
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Typeface
import android.graphics.drawable.ColorDrawable
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.widget.TextView
import com.custom.base.util.SDViewUtil
import com.custom.base.util.SDViewUtil.fastClick
import com.custom.base.R

class SideBar(context: Context, attrs: AttributeSet) : View(context, attrs) {
    private var onTouchingLetterChangedListener: OnTouchingLetterChangedListener? = null
    private var choose = -1 // 选中位置
    private val paint = Paint()
    private var textSize = 14
    private var mTextDialog: TextView? = null

    private var b = listOf<String>()

    fun setTextView(mTextDialog: TextView?) {
        this.mTextDialog = mTextDialog
    }

    init {
        val selectAttrs: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.SideBar)
        textSize = selectAttrs.getInteger(R.styleable.SideBar_SideBar_textSize,14)
        val abc = selectAttrs.getString(R.styleable.SideBar_SideBar_abc).toString()
        b = abc.split(",")
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val height = height
        val width = width
        val singleHeight = height / b.size
        for (i in b.indices) {
            paint.color = Color.parseColor("#5D5D5D")
            paint.typeface = Typeface.DEFAULT_BOLD
            paint.isAntiAlias = true
            paint.textSize = SDViewUtil.dp2pxInt(textSize).toFloat()
            if (i == choose) {
                paint.color = Color.parseColor("#43b2e6")
                paint.isFakeBoldText = true
            }
            // x坐标等于中间-字符串宽度的一半.
            val xPos = width / 2 - paint.measureText(b[i]) / 2
            val yPos = singleHeight * i + singleHeight.toFloat()
            canvas.drawText(b[i], xPos, yPos, paint)
            paint.reset()
        }
    }

    override fun dispatchTouchEvent(event: MotionEvent): Boolean {
        val action = event.action
        val y = event.y
        val oldChoose = choose
        val listener = onTouchingLetterChangedListener
        val c = (y / height * b.size).toInt() // 点击y坐标所占总高度的比例*b数组的长度就等于点击b中的个数.
        Log.d("dispatchTouchEvent", action.toString() + "")
        when (action) {
            MotionEvent.ACTION_UP -> {
                setBackgroundDrawable(ColorDrawable(0x00000000))
                choose = -1 //
                invalidate()
                if (mTextDialog != null) {
                    mTextDialog!!.visibility = INVISIBLE
                }
            }
            MotionEvent.ACTION_DOWN -> if (fastClick()) {
                setBackgroundResource(R.drawable.sidebar_background)
                if (oldChoose != c) {
                    if (c >= 0 && c < b.size) {
                        listener?.onTouchingLetterChanged(c,b[c])
                        if (mTextDialog != null) {
                            mTextDialog!!.text = b[c]
                            mTextDialog!!.visibility = VISIBLE
                        }
                        choose = c
                        invalidate()
                    }
                }
            }
            MotionEvent.ACTION_MOVE -> {
                setBackgroundResource(R.drawable.sidebar_background)
                if (oldChoose != c) {
                    if (c >= 0 && c < b.size) {
                        listener?.onTouchingLetterChanged(c,b[c])
                        if (mTextDialog != null) {
                            mTextDialog!!.text = b[c]
                            mTextDialog!!.visibility = VISIBLE
                        }
                        choose = c
                        invalidate()
                    }
                }
            }
        }
        return true
    }

    fun setOnTouchingLetterChangedListener(onTouchingLetterChangedListener: OnTouchingLetterChangedListener) {
        this.onTouchingLetterChangedListener = onTouchingLetterChangedListener
    }

    interface OnTouchingLetterChangedListener {
        fun onTouchingLetterChanged(index:Int,s: String)
    }
}